<?php
	namespace App\Controller;
	use \Psr\Http\Message\ServerRequestInterface as Request;
    use \Psr\Http\Message\ResponseInterface as Response;
    use App\Model\UserModel;
	class UserController extends BaseController{
		public function Login($request, $response, $args) {
          include APP_PATH.'/View/User/login.html';
       }
       public function Motai($request, $response, $args) {
          include APP_PATH.'/View/motai.html';
       }
       public function GetName($request, $response, $args){
       	   $user=new UserModel();
       	   $email=$_SESSION['UserEmail'];
       	   $res=$user->selectName($email);
       	   echo json_encode([
       	      'code'=>$res
       	   ]);
       }
       public function getMenu($request, $response, $args){
       	$user=new UserModel();
			$sql="SELECT menu.MenuName,lv FROM user
			INNER JOIN rolr
			ON `user`.RolrID=rolr.ID
			INNER JOIN rolrpower
			ON rolrpower.RolrID=rolr.ID
			INNER JOIN power
			ON rolrpower.Power=power.ID
			INNER JOIN menu
			ON menu.ID=ActionId
			WHERE Email='".$_SESSION['UserEmail']."' AND ActionType='menu' order by lv";
			$res=$user->getMenu($sql);
			echo json_encode($res);
       }
        public function postLogin($request, $response, $args) {
        	$user=new UserModel();
        	$ret=$user->selectAll();
        	$email=$request->getParam("Email");
	        $password=$request->getParam("password");
	        $_SESSION['UserEmail']=$email;
	        $index=0;
	        for($i=0;$i<count($ret);$i++){
	        	if($email==$ret[$i]['Email']&&$password==$ret[$i]['Password']){
	        		break;
	        	}
	        	$index++;
	        }
	        if($index==count($ret)){
	        	echo json_encode([
	        	'code'=>1
	        	]);
	        }else{
	        	echo json_encode([
	        	'code'=>0
	        	]);
	        }
        }
        public function Logout($request, $response, $args) {
           unset($_SESSION['UserEmail']);
          include APP_PATH.'/View/User/login.html';
        }
        public function UserAdd($request, $response, $args) {
          $UserName=$request->getParam('username');
          $NickName=$request->getParam('nickname');
          $Password=$request->getParam('email');
          $Email=$request->getParam('email');
          $Phone=$request->getParam('phone');
          $Sex=$request->getParam('sex');
          $Address=$request->getParam('address');
          $RolrID=$request->getParam('rolrid');
          $data=array();
          $data['UserName']=$UserName;
          $data['NickName']=$NickName;
          $data['Password']=$Password;
          $data['Email']=$Email;
          $data['Phone']=$Phone;
          $data['Sex']=$Sex;
          $data['Address']=$Address;
          $data['RolrID']=$RolrID;
          $user=new UserModel();
          $ret=$user->add($data);
          echo json_encode([
          	'code'=>$ret
          ]);
        }
        public function UserSelect($request, $response, $args){
        	$NickName=$request->getParam('NickName');
        	$draw=$request->getParam('draw')*1;//第几次请求
			$start=$request->getParam('start');//第一条数据开始的索引
			$length=$request->getParam('length');//返回的数据长度
		    $col=$_GET['order'][0]['column'];//那一列排序
			$dir=$_GET['order'][0]['dir'];//升序还是降序
			$sql="select ID,UserName,NickName,Email,Phone,Sex,Address,RolrID from user where nickname like '".$NickName."%' or nickname like '%".$NickName."%'";
		    $orderSql = "";
		    if(isset($col)){
			    $i = intval($col);
			    switch($i){
			    	case 0;$orderSql = " order by id ".$dir;break;
			    	case 1;$orderSql = " order by username ".$dir;break;
			        case 2;$orderSql = " order by nickname ".$dir;break;
			        case 3;$orderSql = " order by email ".$dir;break;
			        case 4;$orderSql = " order by phone ".$dir;break;
			        case 5;$orderSql = " order by sex ".$dir;break;
			        case 5;$orderSql = " order by address ".$dir;break;
			        case 5;$orderSql = " order by rolrid ".$dir;break;
			        default;$orderSql = '';
			    }
			}
			$sql=$sql.$orderSql;
	     	$user=new UserModel();
      	    $ret=$user->queryAll($sql);
		    $recordsTotal=count($ret);
		    $recordsFiltered = $recordsTotal;
		    $data=array();
		    $h=0;
		    for($i=$start;$i<$start+$length;$i++,$h++){
		    	if(!empty($ret[$i])){
		    		$data[$h]=$ret[$i];
		    	}else{
		    		break;
		    	}
		    }
		    for($i=0,$index=$start+1;$i<count($data);$i++,$index++){
		    	$data[$i]['ID']=$index;
		    }
		    echo json_encode([
		    "draw" => $draw,
		    "recordsFiltered" =>$recordsFiltered,
		    "recordsTotal" =>$recordsTotal,
		    'data'=>$data
		    ]);
//        $Email=$request->getParam('Email');
//        $user=new UserModel();
//        $res=$user->Userselect($NickName,$Email);
//        if(empty($res)){
//        	 echo json_encode([
//             'code'=>1
//          ]);
//        }else{
//        	 echo json_encode([
//             'code'=>$res
//          ]);
//        }
        }
        public function PostUserSelect($request, $response, $args){
            $Email=$request->getParam('Email');
            $sql="select * from `user`where Email='".$Email."'";
            $user=new UserModel();
            $ret=$user->queryAll($sql);
          	 if(empty($ret)){
          	 echo json_encode([
               'code'=>1
            ]);
          }else{
          	 echo json_encode([
               'code'=>0
            ]);
          }
        }
        public function UserUpdate($request, $response, $args){
          $UserName=$request->getParam('UserName');
          $NickName=$request->getParam('NickName');
          $Email=$request->getParam('Email');
          $Phone=$request->getParam('Phone');
          $Sex=$request->getParam('Sex');
          $RolrID=$request->getParam('RolrID');
          $Address=$request->getParam('Address');
          $data=array();
          $data['UserName']=$UserName;
          $data['NickName']=$NickName;
          $data['Email']=$Email;
          $data['Phone']=$Phone;
          $data['Sex']=$Sex;
          $data['Address']=$Address;
          $data['RolrID']=$RolrID;
          $user=new UserModel();
          $res=$user->Userupdate($data,$Email);
          echo json_encode([
               'code'=>$res
            ]);
        }
        public function RolrInfo($request, $response, $args) {
          echo'RolrInfo';
        }
        public function UserList($request, $response, $args){
        	include APP_PATH.'/View/User/userlist.html';
        }
         public function List($request, $response, $args){
        	include APP_PATH.'/View/list.html';
        }
         public function UserInfo($request, $response, $args){
//         $user=new UserModel();
//     	   $email=$_SESSION['UserEmail'];
//     	   $res=$user->selectItem($email);
//     	   echo json_encode([
//     	      'code'=>1
//     	   ]);
           include APP_PATH.'/View/userinfo.html';
        }
        public function User2($request, $response, $args){
        	include APP_PATH.'/View/user2.html';
        }
        public function Index($request, $response, $args){
        	include APP_PATH.'/View/index.html';
        }
        public function UserItem($request, $response, $args){
            $draw=$request->getParam('draw')*1;//第几次请求
			$start=$request->getParam('start');//第一条数据开始的索引
			$length=$request->getParam('length');//返回的数据长度
		    $col=$_GET['order'][0]['column'];//那一列排序
			$dir=$_GET['order'][0]['dir'];//升序还是降序
			$sql="select ID,UserName,NickName,Email,Phone,Sex,Address,RolrID from user";
		    $orderSql = "";
		    if(isset($col)){
			    $i = intval($col);
			    switch($i){
			    	case 0;$orderSql = " order by id ".$dir;break;
			    	case 1;$orderSql = " order by username ".$dir;break;
			        case 2;$orderSql = " order by nickname ".$dir;break;
			        case 3;$orderSql = " order by email ".$dir;break;
			        case 4;$orderSql = " order by phone ".$dir;break;
			        case 5;$orderSql = " order by sex ".$dir;break;
			        case 5;$orderSql = " order by address ".$dir;break;
			        case 5;$orderSql = " order by rolrid ".$dir;break;
			        default;$orderSql = '';
			    }
			}
			$sql=$sql.$orderSql;
	     	$user=new UserModel();
      	    $ret=$user->queryAll($sql);
		    $recordsTotal=count($ret);
		    $recordsFiltered = $recordsTotal;
		    $data=array();
		    $h=0;
		    for($i=$start;$i<$start+$length;$i++,$h++){
		    	if(!empty($ret[$i])){
		    		$data[$h]=$ret[$i];
		    	}else{
		    		break;
		    	}
		    }
		    for($i=0,$index=$start+1;$i<count($data);$i++,$index++){
		    	$data[$i]['ID']=$index;
		    }
		    echo json_encode([
		    "draw" => $draw,
		    "recordsFiltered" =>$recordsFiltered,
		    "recordsTotal" =>$recordsTotal,
		    'data'=>$data
		    ]);
       }
        public function Item($request, $response, $args){
        	$user=new UserModel();
        	$ret=$user->SelectUserList("userlist");
		    $draw=$request->getParam('draw')*1;//第几次请求
			$start=$request->getParam('start');//第一条数据开始的索引
			$length=$request->getParam('length');//返回的数据长度
			$recordsTotal=count($ret);
            $recordsFiltered = $recordsTotal;
		    $data=array();
		    $h=0;
		    for($i=$start;$i<$start+$length;$i++,$h++){
		    	if(!empty($ret[$i])){
		    		$data[$h]=$ret[$i];
		    	}else{
		    		break;
		    	}
		    }
		    echo json_encode([
		    "draw" => $draw,
		    "recordsFiltered" =>$recordsFiltered,
		    "recordsTotal" =>$recordsTotal,
		    'data'=>$data
		    ]);
        }
        public function Update($request, $response, $args){
          $Name=$request->getParam('name');
          $Age=$request->getParam('age');
          $Sex=$request->getParam('sex');
          $Email=$request->getParam('email');
          $Address=$request->getParam('zhuzhi');
          $data=array();
          $data['Name']=$Name;
          $data['Age']=$Age;
          $data['Sex']=$Sex;
          $data['Email']=$Email;
          $data['Address']=$Address;
          $user=new UserModel();
          $res=$user->Lupdate("userlist",$data,$Email);
          	 echo json_encode([
               'code'=>$res
            ]);
        }
        public function Add($request, $response, $args) {
          $Name=$request->getParam('username');
          $Age=$request->getParam('age');
          $Sex=$request->getParam('sex');
          $Email=$request->getParam('email');
          $Address=$request->getParam('zhuzhi');
          $Time=date("Y-m-d");
          $data=array();
          $data['Name']=$Name;
          $data['Age']=$Age;
          $data['Sex']=$Sex;
          $data['Email']=$Email;
          $data['Time']=$Time;
          $data['Address']=$Address;
          $user=new UserModel();
          $ret=$user->Useradd("userlist",$data);
          echo json_encode([
          	'code'=>$ret
          ]);
        }
        public function Select($request, $response, $args){
          $Name=$request->getParam('Name');
          $Email=$request->getParam('Email');
          $user=new UserModel();
          $res=$user->Userselect("userlist",$Name,$Email);
          if(empty($res)){
          	 echo json_encode([
               'code'=>1
            ]);
          }else{
          	 echo json_encode([
               'code'=>$res
            ]);
          }
        }
        public function GetUserAdd($request, $response, $args){
        	include APP_PATH.'/View/useradd.html';
        }
         public function GetUserUpdate($request, $response, $args){
        	include APP_PATH.'/View/userupdate.html';
        }
         public function Error404($request, $response, $args){
        	include APP_PATH.'/View/404.html';
        }
        public function Error405($request, $response, $args){
        	include APP_PATH.'/View/405.html';
        }
	}